package com.mustafaiev.tair.mweather.xml;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import android.content.res.AssetManager;
import android.util.Log;

import com.mustafaiev.tair.mweather.connectivity.ConnectionManager;
import com.mustafaiev.tair.mweather.exception.MWeatherApiException;
import com.mustafaiev.tair.mweather.exception.MWeatherParseException;
import com.mustafaiev.tair.mweather.forecast.Forecast;
import com.mustafaiev.tair.mweather.forecast.Town;

/**
 * Class for parse forecast from XML file Creation date: 28 Aug 2011 16:52:49
 * 
 * @author Tair Mustafaiev
 * 
 */
public class MWeatherXMLParser {

	private static final String GISMETEO_CITIES_XML_FILE = "/gismeteo_cities.xml";

	/**
	 * Returns forecasts list. Creation date: 1 Sep 2011 23:50:22
	 * 
	 * @param cityId
	 * @return {@link ArrayList} of forecasts
	 * @throws MWeatherParseException
	 */
	public ArrayList<Forecast> getParsedForecast(final int cityId)
			throws MWeatherParseException {
		try {
			return this.parseForecast(cityId);
		} catch (final IOException e) {
			throw new MWeatherParseException(e.toString());
		}
	}

	/**
	 * Returns towns list. Creation date: 2 Sep 2011 23:49:45
	 * 
	 * @param asset
	 * @return {@link ArrayList} towns
	 * @throws MWeatherParseException
	 */
	public ArrayList<Town> getParsedTowns(AssetManager asset)
			throws MWeatherParseException {
		try {
			return this.parseTowns(asset);
		} catch (final IOException e) {
			throw new MWeatherParseException(e.toString());
		}
	}

	/**
	 * Method parses forecasts XML data. Creation date: 28 Aug 2011 17:08:53
	 * 
	 * @return Forecast from parsed XML file
	 * @throws IOException
	 * 
	 */
	private ArrayList<Forecast> parseForecast(final int cityId)
			throws IOException {

		final SAXParserFactory saxFactory = SAXParserFactory.newInstance();

		InputStream is = null;

		try {
			final SAXParser parser = saxFactory.newSAXParser();
			final ForecastXMLHandler xmlHandler = new ForecastXMLHandler();
			try {

				is = ConnectionManager.getUrlContent(Integer.toString(cityId));

				parser.parse(is, xmlHandler);

				return xmlHandler.getForecastList();
			} catch (final IOException e) {
				Log.e("ForecastParser, method parse()", e.toString());
			} catch (final MWeatherApiException e) {
				Log.e("ForecastParser, method parse()", e.toString());
			}

		} catch (final ParserConfigurationException e) {
			Log.e("ForecastParser, method parse()", e.toString());
		} catch (final SAXException e) {
			Log.e("ForecastParser, method parse()", e.toString());
		} finally {
			is.close();
		}

		return null;
	}

	/**
	 * Method parses and returns towns. Creation date: 2 Sep 2011 23:47:47
	 * 
	 * @param asset
	 * @return
	 * @throws IOException
	 */
	private ArrayList<Town> parseTowns(AssetManager asset) throws IOException {

		final SAXParserFactory saxFactory = SAXParserFactory.newInstance();

		InputStream is = null;

		try {
			final SAXParser parser = saxFactory.newSAXParser();
			final TownXMLHandler xmlHandler = new TownXMLHandler();
			try {

				is = asset.open(MWeatherXMLParser.GISMETEO_CITIES_XML_FILE);

				parser.parse(is, xmlHandler);

				return xmlHandler.getTowns();
			} catch (final IOException e) {
				Log.e("ForecastParser, method parse()", e.toString());
			}

		} catch (final ParserConfigurationException e) {
			Log.e("ForecastParser, method parse()", e.toString());
		} catch (final SAXException e) {
			Log.e("ForecastParser, method parse()", e.toString());
		} finally {
			is.close();
		}

		return null;

	}
}
